package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.WxMapper;
import com.oracle.pojo.Gly;
import com.oracle.pojo.GlyExample;
import com.oracle.pojo.Wx;
import com.oracle.pojo.WxExample;
import com.oracle.service.WxService;
import com.oracle.vo.GlyVo;
import com.oracle.vo.WxVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxServiceImpl implements WxService {
    @Autowired(required = false)
    private WxMapper wxMapper;

    @Override
    public List<Wx> findList()
    {
        List<Wx> wxes = wxMapper.selectByExample(null);
        return wxes;
    }

    public PageResult findPages(Integer pageNo, Integer pageSize, Wx wx)
    {
        PageHelper.startPage(pageNo,pageSize);

        WxExample example = new WxExample();
        WxExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(wx.getWsId()))
        {
            criteria.andWsIdEqualTo(wx.getWsId());
        }
        if (!ObjectUtils.isEmpty(wx.getSsId()))
        {
            criteria.andSsIdEqualTo(wx.getSsId());
        }
        if (!ObjectUtils.isEmpty(wx.getSsDormbuild()))
        {
            criteria.andSsDormbuildEqualTo(wx.getSsDormbuild());
        }
        if (!ObjectUtils.isEmpty(wx.getWxry()))
        {
            criteria.andWxryEqualTo(wx.getWxry());
        }
        if (!ObjectUtils.isEmpty(wx.getReason()))
        {
            criteria.andReasonEqualTo(wx.getReason());
        }
        if (!ObjectUtils.isEmpty(wx.getCjTime()))
        {
            criteria.andCjTimeEqualTo(wx.getCjTime());
        }
        if (!ObjectUtils.isEmpty(wx.getUpTime()))
        {
            criteria.andUpTimeEqualTo(wx.getUpTime());
        }


        example.setOrderByClause("wx_id");

        Page<Wx> wxPage = (Page<Wx>)wxMapper.selectByExample(example);

        List<Wx> wxList = wxPage.getResult();

        List<WxVo> wxVoList = buildWxVoList(wxList);

        return new PageResult(wxPage.getTotal(),wxVoList);
    }

    private List<WxVo> buildWxVoList(List<Wx> wxList)
    {
        List<WxVo> wxVoList = new ArrayList<>();
        wxList.forEach(wx -> {
            WxVo wxVo2 = new WxVo();

            BeanUtils.copyProperties(wx,wxVo2);

            wxVoList.add(wxVo2);
        });

        return wxVoList;
    }
}
